/*
** f_args.c for f_args in /home/dieng_e/zappy-2011/client_graphique
** 
** Made by el-hadji-malick-ndour dieng
** Login   <dieng_e@epitech.net>
** 
** Started on  Thu Jun 16 19:01:59 2011 el-hadji-malick-ndour dieng
** Last update Tue Jun 21 00:42:29 2011 arnaud1 tessa
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include "client.h"

void Init_struct(t_client_params *client)
{
client->team = NULL;
client->port = NULL;
client->host = "localhost";
}

void   Get_args(int argc, char **argv, t_client_params * client)
{
  int c;
  
  opterr = 0;
  while ((c = getopt (argc, argv, "n:p:h:")) != -1)
    switch (c)
      {
      case 'n':
	client->team = optarg;
	break;
      case 'p':
	client->port = optarg;
	break;
      case 'h':
	client->host = optarg;
	break;
      default:
	exit(EXIT_FAILURE);
      } 
}

void    check_params(int argc, char **argv)
{
  if (argc != 7)
    print_arg_error("Usage:  $>./client -n [name]  -p [port] -h [host]");
  else if (argc == 7)
    {
      int i;

      i = 0;
      while (argv[4][i])
        {
	  if ('0' <= argv[4][i] && argv[4][i] <= '9')
	    ++i;
	  else
	    print_arg_error("port must be a number");
        }
      if (atoi(argv[4]) < 1024 && atoi(argv[4]) > 65555)
	print_arg_error("port must be between 1025 and 65554\n");
    }
}

void    print_arg_error(char *str)
{
  printf("%s\n", str);
  exit(EXIT_FAILURE);
}
